package com.jilefojing.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.jilefojing.base.BaseWeb;
import com.jilefojing.beans.data.request.base.RequestData;
import com.jilefojing.beans.data.response.base.ResponseData;
import com.jilefojing.beans.data.web.response.AudioData;
import com.jilefojing.beans.data.web.response.DCategoryData;
import com.jilefojing.beans.exception.UserContextException;
import com.jilefojing.beans.pojos.RCategorySource;
import com.jilefojing.beans.pojos.enhanced.EnhancedDCategory;
import com.jilefojing.beans.pojos.enhanced.EnhancedEAudio;
import com.jilefojing.beans.vo.Order;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.context.beans.vo.UserContext;
import com.jilefojing.convent.CommonConvent;
import com.jilefojing.service.IDCategoryService;
import com.jilefojing.service.IEAudioService;
import com.jilefojing.service.IRCategorySourceService;
import com.jilefojing.service.IStatisticsService;
import com.jilefojing.util.Detect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("audio")
public class AudioController extends BaseWeb {
    @Autowired
    private IEAudioService eAudioService;
    @Autowired
    private IStatisticsService statisticsService;
    @Autowired
    private IRCategorySourceService rCategorySourceService;
    @Autowired
    private IDCategoryService dCategoryService;

    @PostMapping("categories")
    public ResponseData getCategories() {
        ResponseData responseData = new ResponseData();


        List<EnhancedDCategory> enhancedDCategories = eAudioService.getEnhancedDCategories(null);

        Iterator<EnhancedDCategory> enhancedDCategoryIterator = enhancedDCategories.iterator();
        while (enhancedDCategoryIterator.hasNext()) {
            EnhancedDCategory curEnhancedDCategory = enhancedDCategoryIterator.next();
            if (curEnhancedDCategory.getEnhancedParentDCategory() != null && curEnhancedDCategory.getEnhancedParentDCategory().getId() > 0) {
                enhancedDCategoryIterator.remove();
            }
        }

        dCategoryService.appendEnhancedChildDCategories2EnhancedDCategories(enhancedDCategories, null);

        List<DCategoryData> dCategoryDatas = CommonConvent.convent2DCategoryDatas(enhancedDCategories);
        responseData.setData(dCategoryDatas);

        return responseData;
    }


    /**
     * 分页获取的音乐列表
     *
     * @param requestData
     * @return
     */
    @PostMapping("list")
    public ResponseData list(@RequestBody RequestData requestData) {
        UserContext userContext = null;
        try {
            userContext = super.getUserContext();
        } catch (UserContextException e) {
            // 可忽略
        }

        JSONObject requestJson = requestData.getRequest();


        List<Long> categoryIds = requestJson.getJSONArray("cat").toJavaList(Long.class);
        if (categoryIds.size() == 1 && categoryIds.get(0) == 0) {//0 默认查全部
            categoryIds = null;
        }

        short sort = requestJson.getShort("sort");

        int page = requestJson.getInteger("page");
        int size = requestJson.getInteger("size");

        // cat=1~2~5-31~3~8 --->两种选择交集
        Pagination<EnhancedEAudio> pagination = new Pagination<EnhancedEAudio>(page, size, true);
        if (sort == 1) {// 热门
            pagination.addOrder(new Order(RCategorySource.COLUMN_HOT_SORT, Order.ORDER_DESC));
        } else if (sort == 2) {// 最近更新
            pagination.addOrder(new Order(RCategorySource.COLUMN_UPDATE_TIME, Order.ORDER_DESC));
        }

        pagination = eAudioService.getPaginationByCategoryIdsForActive(categoryIds, null, pagination, userContext);

        ResponseData responseData = new ResponseData();
        Map<String, Object> result = Maps.newHashMap();
        result.put("count", pagination.getCount());
        result.put("page", pagination.getIndex());
        result.put("size", pagination.getSize());

        if (Detect.notEmpty(pagination.getItems())) {
            pagination.setItems(eAudioService.appendEnhancedChildren(pagination.getItems(), userContext));

            List<AudioData> audioDatas = CommonConvent.convent2EAudioDatas(pagination.getItems());

            result.put("items", audioDatas);
        }
        responseData.setData(result);

        return responseData;
    }

}
